package com.example;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: fransvanderlek
 * Date: 6/15/12
 * Time: 11:13 PM
 * To change this template use File | Settings | File Templates.
 */
public class ImageAdapter extends BaseAdapter {
    private List<File> files;
    private Context context;

    public ImageAdapter(List<File> inputFiles, Context inputContext) {
        files = inputFiles;
        context = inputContext;
    }



    public Context getContext(){
        return this.context;
    }


    public int getCount() {
        return files.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout layout;

        if ( convertView==null)
        {
            layout = new LinearLayout(context);
            layout.setOrientation(LinearLayout.VERTICAL);

            ImageView imageView= new ImageView(context);


            imageView.setId(1);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);

            TextView textView = new TextView(context);
            textView.setId(2);

            layout.addView(imageView, 0);


            layout.addView(textView, 1);

        }
        else
        {
            layout = (LinearLayout) convertView;
        }

        ImageView imageView = (ImageView) layout.findViewById(1);


        Log.d("ImageDecoding", "Decoding "+files.get(position).getName());

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap myBitmap = BitmapFactory.decodeFile(files.get(position).getAbsolutePath(), options);

        if (myBitmap != null)
        {
            imageView.setImageBitmap(myBitmap);
        }
        else
        {
           imageView.setImageResource(R.drawable.android_normal);
        }

        TextView textView = (TextView) layout.findViewById(2);
        textView.setText(files.get(position).getName()+" ["+ new FileSizeFormatter().format(files.get(position).length())+"]" );


        return layout;
    }

}
